<template>
  <div class="app-container">
    <el-card class="box-card" shadow="never">
      <el-form :inline="true" :model="dataForm">
        <el-form-item label="公司名称">
          <el-autocomplete
            clearable
            v-model="dataForm.companyName"
            :fetch-suggestions="querySearch"
            placeholder="请输入公司名称"
            :trigger-on-focus="false"
            @select="handleSelect"
          ></el-autocomplete>
        </el-form-item>
        <el-form-item label="申报时间">
          <el-date-picker
            v-model="dataForm.dateTime"
            type="daterange"
            class="time"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptionsItem"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="申报计划所属周期" class="yearWall">
          <el-date-picker
            v-model="dataForm.cycleDateStart"
            type="year"
            value-format="yyyy"
            placeholder="选择年"
          ></el-date-picker>
          <span class="to">至</span>
          <el-date-picker
            v-model="dataForm.cycleDateEnd"
            type="year"
            value-format="yyyy"
            placeholder="选择年"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
        <el-button @click="handleExport" type="warning" style="float:right">自助申报Excel导出</el-button>
      </el-form>
    </el-card>
    <el-card class="box-card" shadow="never">
      <div slot="header">
        <el-button type="primary" @click="handleEdit('new')" icon="el-icon-circle-plus-outline">自助申报</el-button>
        <el-button @click="handleEdit('select')" icon="el-icon-circle-plus-outline">季度用水量</el-button>
      </div>
      <!-- 数据展示 -->
      <el-table
        :data="tableData"
        :header-cell-style="{background:'#f0f3fa',textAlign:'center'}"
        style="width: 100%"
        :row-class-name="tableRowClassName"
        :cell-style="{textAlign:'center'}"
        @sort-change="sortChange"
      >
        <el-table-column prop="companyName" label="单位名称"></el-table-column>
        <el-table-column prop="companyTypeName" label="单位类型"></el-table-column>
        <el-table-column prop="companyAddress" label="单位地址"></el-table-column>
        <el-table-column prop="totalWater" label="用水量(吨/年)" sortable="custom">
          <template slot-scope="scope">
            <span class="bule" @click="handleSee(scope.row)">{{ scope.row.totalWater}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="waterTypeName" label="用水类型"></el-table-column>
        <el-table-column prop="applyDate" label="申报日期"></el-table-column>
        <el-table-column prop="cycleDate" label="申报计划所属周期"></el-table-column>
        <el-table-column label="是否超计划申报">
          <template slot-scope="scope">{{scope.row.planStatus ? '是' :'否'}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" content="编辑">
              <i class="el-icon-edit" @click="handleEdit('edit', scope.row)"></i>
            </el-tooltip>
            <el-tooltip class="item" content="详情">
              <i class="el-icon-info" @click="handleEdit('info',scope.row)"></i>
            </el-tooltip>
            <el-tooltip class="item" content="删除" v-if="isData(scope.row)">
              <i class="el-icon-delete" @click="handleDelete(scope.row)"></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="dataForm.page"
        :page-sizes="[10, 20, 40, 60]"
        :page-size="dataForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <newDeclare ref="newDeclare" @updata="emptyFun"></newDeclare>
    <waterItem ref="waterItem"></waterItem>
    <!-- 季度选择 -->
    <quarterlyWaterSelect ref="quarterlyWaterSelect"></quarterlyWaterSelect>
    <!-- 季度添加 -->
    <quarterlyWaterAdd ref="quarterlyWaterAdd" @updata="emptyFun"></quarterlyWaterAdd>
  </div>
</template>

<script>
import newDeclare from './component/newDeclare.vue'
import quarterlyWaterSelect from './component/quarterlyWaterSelect.vue'
import quarterlyWaterAdd from './component/quarterlyWaterAdd.vue'
import waterItem from '@/components/waterItem/idnex.vue'
import { getAllWaterDeclare, deleteWaterDeclareById,downloadWaterDeclareAll } from '@/api/other'
import { confirm } from '@/config/publicMethod'
export default {
  // 组件注册
  components: {
    newDeclare,
    waterItem,
    quarterlyWaterSelect,
    quarterlyWaterAdd
  },
  data() {
    return {
      dataForm: {
        companyId: '',
        companyName: '',
        dateTime: [], // mixins中this.ShowDate(366), this.ShowDate(0)
        cycleDateStart: '',
        cycleDateEnd: '',
        fieldSort:'',
        page: 1,
        pageSize: 10,
      },
      tableData: [],
      total: 0,
    }
  },
  mounted() {
    this.getAllWaterDeclareReq()
  },

  methods: {
    // querySearch()  该方法在混入中
    getAllWaterDeclareReq() {
      let data = {
        companyId: this.dataForm.companyId,
        applyDateStart: this.dataForm.dateTime.length > 0 ? this.dataForm.dateTime[0] + ' 00:00:00' : '',
        applyDateEnd: this.dataForm.dateTime.length > 0 ? this.dataForm.dateTime[1] + ' 23:59:59' : '',
        cycleDateEnd: this.dataForm.cycleDateEnd,
        cycleDateStart: this.dataForm.cycleDateStart,
        fieldSort: this.dataForm.fieldSort,
        page: this.dataForm.page,
        pageSize: this.dataForm.pageSize,
      }
      if (this.dataForm.cycleDateStart > this.dataForm.cycleDateEnd)
        return this.$message.info('开始年大于结束年')
      getAllWaterDeclare(data).then((res) => {
        const { code, data, msg } = res
        if (code !== '0000') return this.$message.error(msg)
        const { total, list } = data
        this.total = total
        this.tableData = list
      })
    },
    emptyFun() {
      this.dataForm.companyId = ''
      this.dataForm.companyName = ''
      this.dataForm.dateTime = [] // mixins中this.ShowDate(366), this.ShowDate(0)
      this.dataForm.page = 1
      this.dataForm.cycleDateStart = ''
      this.dataForm.cycleDateEnd = ''
      this.getAllWaterDeclareReq()
    },
    onSubmit() {
      this.dataForm.page = 1
      this.getAllWaterDeclareReq()
    },
    handleEdit(type, item) {
      this.$nextTick((_) => {
        if (type == 'select') {
          this.$refs.quarterlyWaterSelect.init()
        } else {
          this.$refs.newDeclare.init(type, item)
        }
      })
    },
    async handleDelete({ id }) {
      const res = await confirm('确定删除该数据')
      if (res) {
        deleteWaterDeclareById({ id }).then((res) => {
          const { code, msg } = res
          if (code !== '0000') return this.$message.error(msg)
          this.emptyFun()
        })
      }
    },
    handleSee({ id }) {
      this.$refs.waterItem.init(id)
    },
    handleSelect(item) {
      this.dataForm.companyId = item.id
    },
    // 监听pageSize改变事件
    handleSizeChange(newSize) {
      this.dataForm.pageSize = newSize
      this.getAllWaterDeclareReq()
    },
    // 监听 页面值 改变的事件
    handleCurrentChange(newPage) {
      this.dataForm.page = newPage
      this.getAllWaterDeclareReq()
    },
    // 用水量筛选
    sortChange(column) {
      const { order } = column
      this.dataForm.fieldSort =  order == 'ascending' ? '`totalWater` asc' : '`totalWater` desc'
      this.onSubmit()
    },
    // 自助申报Excel导出
    handleExport() {
      let data = {
        companyId: this.dataForm.companyId,
        applyDateStart:  this.dataForm.dateTime.length > 0 ? this.dataForm.dateTime[0] + ' 00:00:00' : '',
        applyDateEnd:  this.dataForm.dateTime.length > 0 ? this.dataForm.dateTime[1] + ' 23:59:59' : '',
        cycleDateEnd: this.dataForm.cycleDateEnd,
        cycleDateStart: this.dataForm.cycleDateStart,
        fieldSort: this.dataForm.fieldSort,
      }
      if (this.dataForm.cycleDateStart > this.dataForm.cycleDateEnd)
        return this.$message.info('开始年大于结束年')
      downloadWaterDeclareAll(data).then(res => {
        if (res.size) {
          let url = URL.createObjectURL(new Blob([res]))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.download = this.dataForm.dateTime.length > 0 ? `企业实际用水(${this.dataForm.dateTime[0]}-${this.dataForm.dateTime[1]}).xls` : `企业实际用水.xls`
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
        }
      })
    }
  },
}
</script>

<style lang='scss' scoped>
</style>
